package com.yanqu.road.server.cmd.activity.sonhai;

import com.yanqu.road.entity.activity.sonhai.config.SonHaiFloorConfig;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiEggData;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiRegionUserResp;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C2_SON_HAI_GET_REGION_LIST, desc = "山海伏兽-获取区域LIST")
public class CrossSonHaiGetRegionListCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SonHaiProto.SonHaiGetRegionListReqMsg reqMsg = SonHaiProto.SonHaiGetRegionListReqMsg.parseFrom(packet.getBytes());
        SonHaiProto.SonHaiGetRegionListRespMsg.Builder respMsg = SonHaiProto.SonHaiGetRegionListRespMsg.newBuilder();
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_SON_HAI_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_GET_REGION_LIST, respMsg));
            return;
        }
        //获取区域LIST
        Map<Integer, SonHaiRegionUserResp> userRegionMap = activity.getRegionMap(packet.getPlayerId());
        //构造PB
        respMsg.setRet(0);
        CrossSonHaiUser user = activity.getUser(packet.getPlayerId());
        if (user != null) {
            CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
            if (team != null) {
                respMsg.setMaxRegionId(team.getMaxRegionId());
            }
        }
        for (SonHaiRegionUserResp resp : userRegionMap.values()) {
            SonHaiProto.SonHaiRegionUser.Builder regionUser = SonHaiProto.SonHaiRegionUser.newBuilder();
            regionUser.setRegionId(resp.getRegionId());
            for (long userId : resp.getUserIds()) {
                regionUser.addUserIds(userId);
            }
            //推荐战力
            regionUser.setPower(resp.getPower());
            respMsg.addRegionUser(regionUser);
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_GET_REGION_LIST, respMsg));
    }

    @Override
    public void sendData() {

    }
}
